package com.hqd.ch03.v51.boot.web.servlet.server;

import com.hqd.ch03.v51.boot.convert.DurationUnit;

import java.io.File;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Set;

public class Session {

    private final Cookie cookie = new Cookie();
    private final SessionStoreDirectory sessionStoreDirectory = new SessionStoreDirectory();
    @DurationUnit(ChronoUnit.SECONDS)
    private Duration timeout = Duration.ofMinutes(30);
    private Set<SessionTrackingMode> trackingModes;
    private boolean persistent;
    /**
     * Directory used to store session data.
     */
    private File storeDir;

    public Duration getTimeout() {
        return this.timeout;
    }

    public void setTimeout(Duration timeout) {
        this.timeout = timeout;
    }

    /**
     * Return the {@link SessionTrackingMode session tracking modes}.
     *
     * @return the session tracking modes
     */
    public Set<Session.SessionTrackingMode> getTrackingModes() {
        return this.trackingModes;
    }

    public void setTrackingModes(Set<Session.SessionTrackingMode> trackingModes) {
        this.trackingModes = trackingModes;
    }

    /**
     * Return whether to persist session data between restarts.
     *
     * @return {@code true} to persist session data between restarts.
     */
    public boolean isPersistent() {
        return this.persistent;
    }

    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }

    /**
     * Return the directory used to store session data.
     *
     * @return the session data store directory
     */
    public File getStoreDir() {
        return this.storeDir;
    }

    public void setStoreDir(File storeDir) {
        this.sessionStoreDirectory.setDirectory(storeDir);
        this.storeDir = storeDir;
    }

    public Cookie getCookie() {
        return this.cookie;
    }

    SessionStoreDirectory getSessionStoreDirectory() {
        return this.sessionStoreDirectory;
    }

    /**
     * Available session tracking modes (mirrors
     * {@link javax.servlet.SessionTrackingMode}.
     */
    public enum SessionTrackingMode {

        /**
         * Send a cookie in response to the client's first request.
         */
        COOKIE,

        /**
         * Rewrite the URL to append a session ID.
         */
        URL,

        /**
         * Use SSL build-in mechanism to track the session.
         */
        SSL

    }

    /**
     * Session cookie properties.
     */
    public static class Cookie extends com.hqd.ch03.v51.boot.web.server.Cookie {

        /**
         * Comment for the session cookie.
         */
        private String comment;

        /**
         * Return the comment for the session cookie.
         *
         * @return the session cookie comment
         */
        public String getComment() {
            return this.comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }

    }

}
